package com.example.java8.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * lambda 表达式测试
 * @author qibao
 * @version v0.1 2020/11/12
 */
public class TestLambda0 {
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 30, 10000d),
            new Employee("李四", 81, 3000d),
            new Employee("王五", 55, 6000d),
            new Employee("赵六", 18, 4500d)
    );

    public List<Employee> employeeListByFilter(List<Employee> employees, Filter<Employee> filter) {
        List<Employee> employeeList = new ArrayList<Employee>();
        for (Employee employee : employees) {
            if (filter.filter(employee)) {
                employeeList.add(employee);
            }
        }
        return employeeList;
    }

    // 策略模式
    public void test1() {
        employeeListByFilter(employees, new FilterEmployeeByAge()).forEach(System.out::println);
        employeeListByFilter(employees, new FilterEmployeeBySalary()).forEach(System.out::println);
    }

    //匿名内部类
    public void test2() {
        employeeListByFilter(employees, new Filter<Employee>() {
            @Override
            public boolean filter(Employee employee) {
                return employee.getAge() > 30;
            }
        }).forEach(System.out::println);

        employeeListByFilter(employees, new Filter<Employee>() {
            @Override
            public boolean filter(Employee employee) {
                return employee.getSalarys() > 10000;
            }
        }).forEach(System.out::println);
    }

    //lambda表达式
    public void test3() {
        employeeListByFilter(employees, (e) -> e.getAge() > 30).forEach(System.out::println);
        employeeListByFilter(employees, (e) -> e.getSalarys() > 10000).forEach(System.out::println);
    }


    public static void main(String[] args) {
        TestLambda0 testLambda = new TestLambda0();

        testLambda.test1();
        System.out.println("-----------------------");
        testLambda.test2();
        System.out.println("-----------------------");
        testLambda.test3();
    }

}
